import 'package:boss/common/common.dart';
import 'package:boss/http/api_response.dart';
import 'package:boss/main.dart';
import 'package:boss/models/analysis_detail.dart';
import 'package:boss/models/batch_delete_commodity_result.dart';
import 'package:boss/models/commodity.dart';
import 'package:boss/models/material.dart';
import 'package:boss/models/pay_info_form.dart';
import 'package:boss/models/mch_data.dart';
import 'package:boss/models/store_menu.dart';
import 'package:boss/ui/page/analysis/page/analysisDetail/model/analysis_detail_type.dart';
import 'package:hi_common/hi_common.dart';

import 'url_apis.dart';

Future<ApiResponse> deleteMenuCategory(int menuId) {
  Map<String, dynamic> data = {'menuId': menuId};

  return Constant.isTakeout ? api.deleteTakeoutMenuCategory(data) : api.deleteMenuCategory(data);
}

Future<ApiResponse<List<StoreMenu>>> queryMenuCategory() {
  return Constant.isTakeout ? api.queryTakeoutMenuCategory() : api.queryMenuCategory();
}

Future<ApiResponse<List<Material>>> queryMeterialList() {
  return Constant.isTakeout ? api.queryTakeoutMeterialList() : api.queryMeterialList();
}

Future<ApiResponse<Material>> addMeterial({
  String meterialName,
  int meterialPrice,
}) {
  Map<String, dynamic> data = {
    "meterialName": meterialName,
    "meterialPrice": meterialPrice,
  };

  return Constant.isTakeout ? api.addTakeoutMeterial(data) : api.addMeterial(data);
}

Future<ApiResponse> updateMeterial(Material material) {
  return Constant.isTakeout ? api.updateTakeoutMeterial(material) : api.updateMeterial(material);
}

Future<ApiResponse> addMenuCategory({
  String name,
  int isPackages,
  List<String> meterialIdList,
}) {
  Map<String, dynamic> data = {
    'name': name,
    'meterialIdList': isPackages == 1 ? [] : meterialIdList,
    'isPackages': isPackages,
  };
  return Constant.isTakeout ? api.addTakeoutMenuCategory(data) : api.addMenuCategory(data);
}

Future<ApiResponse> updateMenuCategory({
  StoreMenu data,
  int isPackages,
}) {
  if (data.isPackages == 1) {
    data.commodityMeterialList = [];
  }
  return Constant.isTakeout ? api.updateTakeoutMenuCategory(data) : api.updateMenuCategory(data);
}

Future<ApiResponse> deleteCommodity(int commodityId) {
  Map<String, dynamic> data = {
    'commodityId': commodityId,
    'status': 0,
  };

  return Constant.isTakeout ? api.deleteTakeoutCommodity(data) : api.deleteCommodity(data);
}

Future<ApiResponse<List<BatchDeleteCommodityResult>>> batchDeleteCommodity(List<int> commodityIds) {
  Map<String, dynamic> data = {'commodityIds': commodityIds};
  return Constant.isTakeout ? api.batchDeleteTakeoutCommodity(data) : api.batchDeleteCommodity(data);
}

Future<ApiResponse> batchMoveCommodity({
  List<int> commodityIdList,
  int menuId,
}) {
  Map<String, dynamic> data = {
    'commodityIdList': commodityIdList,
    'menuId': menuId,
  };
  return Constant.isTakeout ? api.batchMoveTakeoutCommodity(data) : api.batchMoveCommodity(data);
}

Future<ApiResponse> commodityAddNew(Commodity data) {
  return Constant.isTakeout ? api.addTakeoutCommodity(commodity2HttpBody(data)) : api.addCommodity(commodity2HttpBody(data));
}

Future<ApiResponse> updateCommodity(Commodity data) {
  return Constant.isTakeout ? api.updateTakeoutCommodity(commodity2HttpBody(data)) : api.updateCommodity(commodity2HttpBody(data));
}

Future<ApiResponse> sortMenuCategory(List<int> sortIdList) {
  Map<String, dynamic> data = {'sortIdList': sortIdList};

  return Constant.isTakeout ? api.sortTakeoutMenuCategory(data) : api.sortMenuCategory(data);
}

Future<ApiResponse> addPackage(Commodity data) {
  return Constant.isTakeout ? api.addTakeoutPackage(commodity2HttpBody(data)) : api.addPackage(commodity2HttpBody(data));
}

Future<ApiResponse> updatePackage(Commodity data) {
  return Constant.isTakeout ? api.updateTakeoutPackage(commodity2HttpBody(data)) : api.updatePackage(commodity2HttpBody(data));
}

Map<String, dynamic> commodity2HttpBody(Commodity data) {
  Map<String, dynamic> map = {
    'addCommodityMeterialFormList': data.commodityMeterialList,
    'addCommodityTasteFormList': data.commodityTasteList,
    'addSkuFormList': data.commodityType == 4
        ? []
        : data.commoditySkuList
            .map((sku) => {
                  'commodityBarCode': sku.commoditySkuCode,
                  'skuId': sku.skuId,
                  'skuMemberPrice': sku.skuMemberPrice,
                  'skuName': sku.skuName,
                  'skuPrice': sku.skuPrice,
                  'skuOriginalPrice': sku.skuOriginalPrice,
                })
            .toList(),
    'categoryId': data.categoryId,
    'categoryName': data.categoryName,
    'commodityBarCode': data.commodityBarCode,
    'commodityChefPrint': data.commodityChefPrint,
    'commodityCode': data.commodityCode,
    'commodityMinPie': data.commodityMinPie,
    'commodityType': data.commodityType,
    'description': data.description,
    'printerInfoList': data.printerInfoList,
    'images': data.images,
    'memberPrice': data.memberPrice,
    'menuId': data.menuId,
    'menuName': data.menuName,
    'price': data.price,
    'originalPrice': data.originalPrice,
    'buyCount': data.buyCount,
    'title': data.title,
    'subTitle': data.subTitle,
    'unit': data.unit,
    'meterialType': data.meterialType,
    'packaged': data.packaged,
    'packagesGroupIds': data.packagesGroupList.map((item) => item.id).toList(),
    'packagesTag': data.packagesTag,
    'commodityFormList': data.mustCommodityList,
    'commodityId': data.id,
    'onsale': data.onsale,
    'isH5Sell': data.isH5Sell,
    'commodityCellTime': data.commodityCellTime,
    'commodityCellWeek': data.commodityCellWeek,
    'commodityCellDate': data.commodityCellDate,
  };

  return map;
}

Future<ApiResponse> deletePackage(int commodityId) {
  return Constant.isTakeout ? api.deleteTakeoutPackage(commodityId) : api.deletePackage(commodityId);
}

Future<ApiResponse> updateCommodityStock(int commodityId, int stock) {
  final data = {
    'commodityId': commodityId,
    'stock': stock,
  };
  return Constant.isTakeout ? api.updateTakeoutCommodityStock(data) : api.updateCommodityStock(data);
}

Future<ApiResponse> deleteMeterial(int id) {
  return Constant.isTakeout ? api.deleteTakeoutMeterial(id) : api.deleteMeterial(id);
}

Future<ApiResponse<MchData>> addOrUpdateMchInfo(PayInfoForm data) {
  return data.id == null ? api.addMchInfo(data) : api.updateMchInfo(data);
}

//提成明细
Future<ApiResponse<AnalysisDetail>> queryStaffAnalysisDetailForTime({
  String startTime,
  String endTime,
  AnalysisDetailType type,
}) async {
  var url;

  if (type == AnalysisDetailType.takeoutSellSort) {
  } else if (type == AnalysisDetailType.tipSort) {
    url = Apis.queryStoreTipDetailForTime;
  } else if (type == AnalysisDetailType.commodityCommissionSort) {
    url = Apis.queryStoreRoyaltyDetailForTime;
  } else if (type == AnalysisDetailType.staffCommissionList) {
    url = Apis.queryStaffBookCommissionDetailForTime;
  }

  var res = await DioUtil.instance.request(
    url,
    methods: Methods.get,
    queryParameters: {
      'startTime': startTime,
      'endTime': endTime,
    },
  );

  return ApiResponse(code: res['code'], msg: res['msg'], data: AnalysisDetail.fromJson(res['data']));
}

//统计发送邮件
Future analyticSMS({
  String email,
  String startTime,
  String endTime,
  String url,
}) async {
  return DioUtil.instance.request(
    url,
    methods: Methods.get,
    queryParameters: {
      'email': email,
      'startTime': startTime,
      'endTime': endTime,
    },
  );
}
